package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page,queryVo);
    }

    /**
     * 实现思路:
     *     1.如果用户传递了id  则先删除中间表数据
     *     2.新增/更新房间信息
     *     3.新增中间表数据
     * @param roomSubmitVo
     */
    @Override
    @Transactional
    public void saveItem(RoomSubmitVo roomSubmitVo) {
        //1.获取id号
        if(roomSubmitVo.getId() !=null){
            //1.1删除图片信息.
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoQueryWrapper);

            //1.2 删除房间配套信息
            LambdaQueryWrapper<RoomFacility> roomFacilityFacilityQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityFacilityQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityFacilityQueryWrapper);

            //1.3删除房间标签
            LambdaQueryWrapper<RoomLabel> roomLabelQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelQueryWrapper);

            //1.4删除房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueQueryWrapper);

            //1.5删除支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

            //1.6 删除租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);
        }


        //新增更新房间信息
        this.saveOrUpdate(roomSubmitVo);
        //新增之后获取主键
        long id = roomSubmitVo.getId();

        //2.新增图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
        for (GraphVo graphVo : graphVoList){
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemId(id);
            graphInfo.setItemType(ItemType.ROOM);
            graphInfoList.add(graphInfo);
        }
        //提供了批处理 只有通用service有批处理
        graphInfoService.saveBatch(graphInfoList);

        //3.入库房间配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = new ArrayList<>(facilityInfoIds.size());
        for(long facilityInfoId : facilityInfoIds){
            RoomFacility roomFacility =RoomFacility.builder()
                    .roomId(id)
                    .facilityId(facilityInfoId).build();
            roomFacilityList.add(roomFacility);
        }
        roomFacilityService.saveBatch(roomFacilityList);

        //4.新增标签
        List<Long> labelIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel>roomLabelList = new ArrayList<>(labelIds.size());
        for(long labelId : labelIds){
            RoomLabel roomLabel = RoomLabel.builder().roomId(id).labelId(labelId).build();
            roomLabelList.add(roomLabel);
        }
        roomLabelService.saveBatch(roomLabelList);

        //5.保存新的roomAttrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        //5.保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //6.保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTerms.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //1.查询主表的房间信息
        RoomInfo roomInfo = this.getById(id);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        //2.获取图片列表
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,ItemType.ROOM);
        List<GraphInfo> infoList = graphInfoService.list(graphQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>(infoList.size());
        for (GraphInfo graphInfo : infoList){
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVoList.add(graphVo);
        }
        roomDetailVo.setGraphVoList(graphVoList);

        //3.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);


        //4.获取房间属性
        List<AttrValueVo> attrValueVoList =  attrValueMapper.selectByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //5.获取配套列表
        List<FacilityInfo> facilityInfoList =facilityInfoMapper.selectByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        //6.获取标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        //7.获取支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        //8.租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);

        return roomDetailVo;
    }


}




